<?php
/**
 * 工单管理
 * Created by PhpStorm.
 * User: 北山道人
 * Date: 2018/6/29
 * Time: 15:59
 */
namespace app\admin\controller;
use think\Request;

class Works extends Common
{
    /**
     * 工单列表页面
     * @return mixed
     */
    public function workListUI()
    {
        return $this->fetch('workList');
    }

    /**
     * 历史工单页面
     * @return mixed
     */
    public function compWorkUI(){
        return $this->fetch('compWork');
    }

    /**
     * 工单类型
     * @return mixed
     */
    public function typeUI(){
        return $this->fetch('type');
    }

    /**
     * 创建工单页面
     * @return mixed
     */
    public function addUI(){
        return $this->fetch('add');
    }

    /**
     * 工单列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function queryPage(Request $request)
    {
        $input = $request->param();

        $input['gid'] = session('gid');

        $rows = db('works')->alias('w')->where(function ($query)use($input){

            if(!empty($input['id'])){
                $query->where('w.id',$input['id']);
            }

            if(!empty($input['number'])){
                $query->where('w.number',$input['number']);
            }
            if(!empty($input['fault_id'])){
                $query->where('w.fault_id',$input['fault_id']);
            }
            if(!empty($input['state_id'])){
                $sis = explode(',',$input['state_id']);
                $query->where('w.state_id','in',$sis);
            }
            if(!empty($input['tel'])){
                $query->where('w.tel',$input['tel']);
            }
            if(!empty($input['type_id'])){
                $query->where('w.type_id',$input['type_id']);
            }
            if(isset($input['is_inner'])){
                $query->where('w.is_inner',$input['is_inner']);
            }
            if($input['gid']==5){
                $query->where('w.admin_id',session('aid'));
            }
            if(!empty($input['key'])){
                $query->where('w.title','LIKE','%'.$input['key'].'%');
                $query->whereOr('w.number',$input['key']);
            }
        })
        ->join('state st','st.id=w.state_id','left')
        ->join('works_type wt','wt.id=w.type_id','left')
        ->join('fault ft','ft.id=w.fault_id','left')
        ->field(['w.*','st.state_name','wt.type_name','ft.title as fault_name'])
        ->paginate($input['limit'])
        ->toArray();

        return ['code'=>0,'msg'=>'获取成功!','data'=>$rows['data'],'count'=>$rows['total'],'rel'=>1];
    }

    /**
     * 创建工单
     * @param Request $request
     * @return array
     */
    public function add(Request $request){
        $input = $request->param();

        $msg = $this->validate($input,'app\admin\validate\Dispatch');
        if($msg!='true'){
            return ['code'=>0,'msg'=>$msg];
        }

        if(session('gid') == 5){
            $input['state_id'] = 11;
            $input['admin_id'] = session('aid');
        }else{
            $input['state_id'] = 10;
        }

        $input['number'] = date('YmdHis').rand(100,999);

        $row = db('works')->insertGetId($input);

        if($row)
            return ['code'=>1,'msg'=>'添加成功'];
        return ['code'=>0,'msg'=>'添加失败'];

    }

    /**
     * 客服审核
     * @param Request $request
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function audit(Request $request)
    {
        $id = $request->param('id');
        $aid = $request->param('aid');
        if(empty($id)) return ['code'=>0,'msg'=>'id参数错误'];
        if(empty($aid)) return ['code'=>0,'msg'=>'请选择工程师'];
        $row = db('works')->where('id',$id)->update(['admin_id'=>$aid,'state_id'=>11]);
        if($row)
            return ['code'=>1,'msg'=>'审核成功'];
        return ['code'=>0,'msg'=>'审核失败'];
    }
    
    /**
     * 关闭工单
     * @param Request $request
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function close(Request $request){
        $ids = $request->param('ids');
        $idsArr = explode(',',$ids);
        if(count($idsArr)<=0) return ['code'=>0,'msg'=>'ids参数错误'];
        $row = db('works')->where('id','in',$idsArr)->update(['state_id'=>14]);
        if($row)
            return ['code'=>1,'msg'=>'关闭成功'];
        return ['code'=>0,'msg'=>'关闭失败'];
    }

    /********************工单类型操作******************/

    /**
     * 工单类型列表
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function typeList(){
        return $rows = db('works_type')->order('sort asc')->select();
    }

    /**
     * 工单类型添加
     * @param Request $request
     * @return array
     */
    public function typeAdd(Request $request)
    {
        $input = [
            'type_name'=>$request->param('type_name'),
            'sort'=>$request->param('sort')
        ];
        $row = db('works_type')->insertGetId($input);
        if($row)
            return ['code'=>1,'msg'=>'添加成功'];
        return ['code'=>0,'msg'=>'添加失败'];
    }

    /**
     * 工单类型修改
     * @param Request $request
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function typeEdit(Request $request)
    {
        $input = $request->param();
        if(empty($input['id'])) return ['code'=>0,'msg'=>'id参数错误'];
        $row = db('works_type')->where('id',$input['id'])->update($input);
        if($row)
            return ['code'=>1,'msg'=>'修改成功'];
        return ['code'=>0,'msg'=>'修改失败'];
    }

    /**
     * 工单类型删除
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function typeDel(Request $request){
        $input = $request->param();
        if(empty($input['id'])) return ['code'=>0,'msg'=>'id参数错误'];
        $row = db('works_type')->where('id',$input['id'])->delete();
        if($row)
            return ['code'=>1,'msg'=>'删除成功'];
        return ['code'=>0,'msg'=>'删除失败'];
    }



}